function [tangential_velocity, normal_velocity] = w2tn ( w, x, y, vangle )
% W2TN:  converts complex velocity to tangential and normal components wrt a vector
%
% USAGE:  [tangential_velocity, normal_velocity] = w2tn ( w, vangle, x, y );
%
% PARAMETERS:
% Input:
%     w:  complex vector of velocities along a line thru a velocity field
%     x, y:  points defining a line thru the velocity field.
%     vangle:  angle of the roms grid along the line
% Output:
%     tangential velocity:
%     normal_velocity:
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name: rslice-0_9_2 $
% $Id: w2tn.m,v 1.3 2005/06/24 14:13:43 jevans Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


tangential_velocity = [];
normal_velocity = [];

n = length(x);

%
% figure the orientation of the vector from point to point
dx = diff(x);
dy = diff(y);
dx(n) = dx(n-1);
dy(n) = dy(n-1);


%
% Can't quite wrap my head around the reason why I have to
% do this, but it makes the results look right.
theta = (2*pi - atan2(dy,dx));


%
% Rotate the velocity out of the grid
if ~isempty(vangle)
	for j= 1:n
		w(:,j) = w(:,j) * exp ( sqrt(-1) * vangle(j) );
	end
end

%
% Now rotate wrt the angle vector formed by the vslice line.  This
% gives a complex vector with the real component being the
% the tangential velocity, and the imaginary component being
% the normal component.
for j= 1:n
	wvector(:,j) = w(:,j) * exp ( sqrt(-1) * theta(j) );
end
wvectorc = conj(wvector);
tangential_velocity = real(wvectorc);
normal_velocity = imag(wvectorc);


return;



